from dataclasses import asdict

from framework.common_http import Request
from model.goods import Goods
from utils.log import logger


class CartApi:
    def cart_index(self, u_token):
        # 购物车列表接口
        request = Request(
            method="GET",
            host="https://litemall.hogwarts.ceshiren.com",
            path="/wx/cart/index",
            type="json"
        )

        response = request.send(u_token)
        logger.debug(f"cart_index_response: {response.json()}")
        return response.json()

    def cart_add(self, goods: Goods, u_token):
        # 添加购物车接口
        request = Request(
            method="POST",
            host="https://litemall.hogwarts.ceshiren.com",
            path="/wx/cart/add",
            type="json",
            json=asdict(goods)
        )

        response = request.send(u_token)
        logger.debug(f"cart_add_response: {response.json()}")
        return response.json()

    def cart_goods_id_list(self, u_token):
        cart_goods = self.cart_index(u_token).get("data").get("cartList")
        goods_id_list = []
        for goods in cart_goods:
            goods_id_list.append(goods.get("goodsId"))

        return goods_id_list
